using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UMC.Data;
using UMC.Web;
using UMC.Web.UI;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{

    public class WorkFlowWorkActivity : WebActivity
    {

        void Start(UISection ui, int start, int limit, String keywrok)
        {
            var userName = this.Context.Token.Username;

            var entity = HotCache.Search<TaskRequest>();

            if (String.IsNullOrEmpty(keywrok) == false)
            {
                entity.Or().Like(new TaskRequest
                {
                    Username = keywrok,
                    Subject = keywrok,
                    Description = keywrok
                });
            }
            var applys = entity.Query(new TaskRequest
            {
                Username = userName
            }, false, start, limit, out var outIndex);
            var req = this.Context.Request;

            ui.StartIndex = outIndex;
            ui.IsNext = outIndex > -1;


            var reqArg = req.Arguments.GetDictionary();
            foreach (var apply in applys)
            {
                var data = new WebMeta();
                data["icon"] = "\uf007";
                data["ticon"] = "\uf073";
                data["time"] = String.Format("{0:yyyy.MM.dd HH:mm}", apply.ActiveTime);
                data["title"] = apply.Subject;

                var taskForm = DataFactory.Instance().TaskForm(apply.TaskId.Value);

                var cell = new UICMS(new UIClick(req.Model,  $"Form.{taskForm.Code}", new WebMeta(reqArg).Put("InstanceId", apply.InstanceId)), data);


                data["name"] = apply.ActiverAlias;

                var style = cell.Style.Name("tag");
                switch (apply.Status.Value)
                {
                    case TaskStatus.Completed:
                        style.BgColor(0x459df5);
                        cell.Tag("已完结");
                        break;
                    case TaskStatus.Locked:
                        style.BgColor(0x459df5);
                        cell.Tag("已完结");
                        break;
                    case TaskStatus.InReview:
                        style.BgColor(0x47a372);
                        cell.Tag("审批中");
                        break;
                    case TaskStatus.Prepare:
                        style.BgColor(0x47a372);
                        cell.Tag("准备中");
                        break;
                    case TaskStatus.Termination:
                        style.BgColor(0xBF3030);
                        cell.Tag("已终止");
                        break;
                }


                cell.Title("{title}");

                cell.Left("{icon} {name}");
                cell.Right("{ticon} {time}");
                cell.Style.Name("icon").Font("wdk").Size(12);
                cell.Style.Name("ticon").Font("wdk").Size(12);
                ui.Add(cell);
            }
            if (start == 0 && applys.Length == 0)
            {

                ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有发起").Put("icon", "\uEA05"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                   new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 
            }

        }
        void Notity(UISection ui, int start, int limit, String keywrok)
        {
            var userName = this.Context.Token.Username;
            var entity = HotCache.Search<TaskMessage>();
            entity.Greater(new TaskMessage { Type = 1 });

            if (String.IsNullOrEmpty(keywrok) == false)
            {
                entity.Or().Like(new TaskMessage
                {
                    Username = keywrok,
                    Alias = keywrok,
                    Subject = keywrok,
                    Body = keywrok
                });
            }
            var applys =
            entity.Query(new TaskMessage
            {
                Username = userName

            }, true, start, limit, out var outIndex).OrderByDescending(r => r.CreationTime);

            var req = this.Context.Request;
            ui.StartIndex = outIndex;
            ui.IsNext = outIndex > -1;

            var reqArg = req.Arguments.GetDictionary();
            foreach (var apply in applys)
            {
                var data = new WebMeta();
                data["icon"] = "\uf007";
                data["ticon"] = "\uf073";
                data["time"] = String.Format("{0:yyyy.MM.dd HH:mm}", apply.CreationTime);
                data["title"] = apply.Subject;

                var taskForm = DataFactory.Instance().TaskForm(apply.TaskId.Value);

                var cell = new UICMS(new UIClick(req.Model, $"Form.{taskForm.Code}", new WebMeta(reqArg).Put("InstanceId", apply.InstanceId)), data);


                data["name"] = apply.ProposerAlias ?? apply.Proposer;



                cell.Left("{icon} {name}");
                cell.Right("{ticon} {time}");
                cell.Style.Name("icon").Font("wdk").Size(12);
                cell.Style.Name("ticon").Font("wdk").Size(12);
                ui.Add(cell);
            }
            if (start == 0 && applys.Count() == 0)
            {

                ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有知会").Put("icon", "\uEA05"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                   new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 
            }

        }
        void Done(UISection ui, int start, int limit, String keywrok)
        {
            var userName = this.Context.Token.Username;

            var entity = HotCache.Search<TaskHistory>();
            if (String.IsNullOrEmpty(keywrok) == false)
            {
                entity.Or().Like(new TaskHistory
                {
                    IdentityName = keywrok,
                    Proposer = keywrok,
                    ProposerAlias = keywrok,
                    Subject = keywrok
                });
            }

            var applys = entity.Query(new TaskHistory
            {

                IdentityName = userName
            }, false, start, limit, out var outIndex);

            ui.StartIndex = outIndex;
            ui.IsNext = outIndex > -1;
            var req = this.Context.Request;

            var reqArg = req.Arguments.GetDictionary();
            foreach (var apply in applys)
            {
                var data = new WebMeta();
                data["icon"] = "\uf007";
                data["ticon"] = "\uf073";
                data["time"] = String.Format("{0:yyyy.MM.dd HH:mm}", apply.RevertTime);
                data["title"] = apply.Subject;

                var taskForm = DataFactory.Instance().TaskForm(apply.TaskId.Value);
                var cell = new UICMS(new UIClick(req.Model, $"Form.{taskForm.Code}", new WebMeta(reqArg).Put("InstanceId", apply.InstanceId)), data);

                data["name"] = apply.ProposerAlias ?? apply.Proposer;
                var style = cell.Style.Name("tag");
                switch (apply.Status.Value)
                {
                    case TaskStatus.Completed:
                        style.BgColor(0x459df5);
                        cell.Tag("已完结");
                        break;
                    case TaskStatus.Locked:
                        style.BgColor(0x459df5);
                        cell.Tag("已完结");
                        break;
                    case TaskStatus.InReview:
                        style.BgColor(0x47a372);
                        cell.Tag("审批中");
                        break;
                    case TaskStatus.Prepare:
                        style.BgColor(0x47a372);
                        cell.Tag("准备中");
                        break;
                    case TaskStatus.Termination:
                        style.BgColor(0xBF3030);
                        cell.Tag("已终止");
                        break;
                }


                cell.Title("{title}");
                cell.Left("{icon} {name}");
                cell.Right("{ticon} {time}");
                cell.Style.Name("icon").Font("wdk").Size(12);
                cell.Style.Name("ticon").Font("wdk").Size(12);
                ui.Add(cell);
            }
            if (start == 0 && applys.Length == 0)
            {

                ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有已办").Put("icon", "\uEA05"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                   new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 
            }

        }
        void Doing(UISection ui, int start, int limit, String keywrok)
        {
            var userName = this.Context.Token.Username;
            var entity = HotCache.Search<TaskApproving>();
            if (String.IsNullOrEmpty(keywrok) == false)
            {
                entity.Or().Like(new TaskApproving
                {
                    Username = keywrok,
                    Alias = keywrok,
                    ProposerAlias = keywrok,
                    Proposer = keywrok,
                    Subject = keywrok,
                    Sender = keywrok,
                    SenderAlias = keywrok
                });
            }
            //var usernames = new List<String>();
            var applys = new List<TaskApproving>();
            entity.Query(new TaskApproving
            {
                Username = userName
            }, false, start, limit, out var outIndex);

            ui.StartIndex = outIndex;
            ui.IsNext = outIndex > -1;
            var req = this.Context.Request;
            var reqArg = req.Arguments.GetDictionary();
            foreach (var apply in applys)
            {
                var  taskForm= DataFactory.Instance().TaskForm(apply.TaskId.Value);
                var data = new WebMeta();
                data["icon"] = "\uf007";
                data["ticon"] = "\uf073";
                data["time"] = String.Format("{0:yyyy.MM.dd HH:mm}", apply.ActiveTime);
                data["title"] = apply.Subject;

                var cell = new UICMS(new UIClick(req.Model, $"Form.{taskForm.Code}", new WebMeta(reqArg).Put("InstanceId", apply.InstanceId)), data);

                data["name"] = apply.ProposerAlias;



                cell.Left("{icon} {name}");
                cell.Right("{ticon} {time}");
                cell.Style.Name("icon").Font("wdk").Size(12);
                cell.Style.Name("ticon").Font("wdk").Size(12);
                ui.Add(cell);
            }
            if (start == 0 && applys.Count == 0)
            {

                ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有待办").Put("icon", "\uEA05"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                   new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 
            }

        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {


            var form = request.SendValues ?? new UMC.Web.WebMeta();


            int limit = UMC.Data.Utility.IntParse(form["limit"] as string, 10);
            int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);
            var Keyword = (form["Keyword"] as string ?? String.Empty);

            var ui = start == 0 ? UISection.Create(new UITitle("我的待办")) : UISection.Create();
            if (start == 0)
            {
                //ui.Title.Right(new UIEventText("新建").Click(new UIClick(request.Model, request.Command, "News")));

                var items = new List<WebMeta>
                {
                    new UMC.Web.WebMeta().Put("text", "待办").Put("search", new WebMeta().Put("Type", "Doing")),
                    new UMC.Web.WebMeta().Put("text", "发起").Put("search", new WebMeta().Put("Type", "Start")),
                    new UMC.Web.WebMeta().Put("text", "已办").Put("search", new WebMeta().Put("Type", "Done")),
                    new UMC.Web.WebMeta().Put("text", "知会").Put("search", new WebMeta().Put("Type", "Notify"))
                };
                ui.Add(UICell.Create("TabFixed", new UMC.Web.WebMeta().Put("items", items))); ;
            }

            switch (this.AsyncDialog("Type", "Doing"))
            {
                case "Doing":
                    this.Doing(ui, start, limit, Keyword);
                    break;
                case "Done":
                    this.Done(ui, start, limit, Keyword);
                    break;
                case "Start":
                    this.Start(ui, start, limit, Keyword);
                    break;
                case "Notify":
                    this.Notity(ui, start, limit, Keyword);
                    break;
            }
            ui.SendTo(this.Context, "WorkFlow.Progress");
        }

    }

}